home *** CD-ROM | disk | FTP | other *** search
- //____________________________________________________________
- // SoundHandle.c
- //
- // Copyright © Dan Parks Sydow, 1995
- // From the book:
- // "Graphics and Sound Programming Techniques for the Mac",
- // M&T Books, 1995
- //
- // The "Graphics and Sound Programming for the Mac" book shows the variable theSound
- // having a data type of SndListHandle. The book does this in preparation for the call
- // to SndRecord(). The book shows that the third parmater of this function as a
- // SndListHandle - as expected by the version of the Sound.h universal header file used
- // by Metrowerks at the time of this writing. Symantec uses a different version of
- // Sound.h. If Symantec updates the Apple Universal Header files, you may get a compilation
- // error. If you do, it will concern code in the PlaySoundResourceSynch() function.
- // Here are the changes you'll need to make. Change the code to match the code in the book.
-
-
-
- //____________________________________________________________
-
- #include <Sound.h>
- #include <SoundInput.h>
-
-
- //____________________________________________________________
-
- void InitializeToolbox( void );
- Boolean IsSoundInputAvailable( void );
- OSErr RecordSoundToMemory( Handle * );
- OSErr PlaySoundSynchFromHandle( Handle );
-
-
- //____________________________________________________________
-
- #define kHeapReserve 75 * 1024
-
-
- //____________________________________________________________
-
- void main( void )
- {
- NumVersion theSndMgrVers;
- OSErr theError;
- Handle theSound;
- Boolean soundInputPresent;
-
- InitializeToolbox();
-
- MaxApplZone();
-
- theSndMgrVers = SndSoundManagerVersion();
- if ( theSndMgrVers.majorRev < 3 )
- ExitToShell();
-
- soundInputPresent = IsSoundInputAvailable();
- if ( soundInputPresent == false )
- ExitToShell();
-
- theError = RecordSoundToMemory( &theSound );
- if ( theError == userCanceledErr )
- ExitToShell();
-
- theError = PlaySoundSynchFromHandle( theSound );
- if ( theError != noErr )
- ExitToShell();
-
- ReleaseResource( (Handle)theSound );
- }
-
-
- //____________________________________________________________
-
- OSErr RecordSoundToMemory( Handle *theSound )
- {
- OSErr theError;
- Point theCorner = { 50, 20 };
- long theTotalHeap;
- long theContigMem;
-
- PurgeSpace( &theTotalHeap, &theContigMem );
-
- *theSound = NewHandle( theContigMem - kHeapReserve );
-
- theError = SndRecord( nil, theCorner, siBestQuality, theSound );
-
- return ( theError );
- }
-
-
- //____________________________________________________________
-
- OSErr PlaySoundSynchFromHandle( Handle theHandle )
- {
- OSErr theError;
-
- if ( theHandle != nil )
- {
- HLock( theHandle );
- theError = SndPlay( nil, theHandle, false );
- HUnlock( theHandle );
-
- return ( theError );
- }
- }
-
-
- //____________________________________________________________
-
- Boolean IsSoundInputAvailable( void )
- {
- OSErr theError;
- long theResult;
- Boolean inputAvail;
-
- theError = Gestalt( gestaltSoundAttr, &theResult );
- if ( theError != noErr )
- ExitToShell();
-
- inputAvail = theResult & ( 1 << gestaltHasSoundInputDevice );
- if ( inputAvail > 0 )
- return ( true );
- else
- return ( false );
- }
-
-
- //____________________________________________________________
-
- void InitializeToolbox( void )
- {
- InitGraf( &qd.thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0L );
- FlushEvents( everyEvent, 0 );
- InitCursor();
- }
-